//
// Created by yyl on 2018/6/5.
//

#include "yyl_player.h"
#include "CallJava.h"
#include "YFFmpeg.h"

CallJava *callJava = NULL;
YFFmpeg *ffmpeg = NULL;
PlayStatus *playStatus = NULL;
bool exitEnable = true;

pthread_t thread_start;

extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_prepare(JNIEnv *env, jobject instance, jstring source_) {
    const char *source = env->GetStringUTFChars(source_, 0);
    if (ffmpeg == NULL && exitEnable) {

        if (callJava == NULL)
            callJava = new CallJava(env, env->NewGlobalRef(instance));
        callJava->onCallBackEvent(THREAD_MAIN, EVENT_STATE_PARPARED, EVENT_FALSE, 0);
        playStatus = new PlayStatus();
        ffmpeg = new YFFmpeg(callJava, source, playStatus);
        ffmpeg->parpared();
    }
    //  env->ReleaseStringUTFChars(source_, source);
}


void *startPlayAudio(void *data) {
    YFFmpeg *fFmpeg = (YFFmpeg *) data;
    fFmpeg->start();
    pthread_exit(&thread_start);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_play(JNIEnv *env, jobject instance) {

    if (ffmpeg != NULL) {
        pthread_create(&thread_start, NULL, startPlayAudio, ffmpeg);
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_changePlayState(JNIEnv *env, jobject instance, jboolean isPlay) {
    if (ffmpeg != NULL) {
        ffmpeg->changePlayState(isPlay);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_stop(JNIEnv *env, jobject instance) {
    if (!exitEnable)return;
    exitEnable = false;
    if (ffmpeg != NULL) {
        ffmpeg->release();
        delete (ffmpeg);
        ffmpeg = NULL;
        if (callJava != NULL) {
            delete (callJava);
            callJava = NULL;
        }
        if (playStatus != NULL) {
            delete (playStatus);
            playStatus = NULL;
        }
    }
    LOGI("YPlayer_stop")
    exitEnable = true;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_seek(JNIEnv *env, jobject instance, jlong time) {
    if (ffmpeg != NULL) {
        ffmpeg->seek(time);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_setAudioOption(JNIEnv *env, jobject instance, jint option,
                                               jint value, jdouble dvalue) {
    if (ffmpeg != NULL) {
        switch (option) {
            case 1:
                ffmpeg->setPitch(dvalue);
                break;
            case 2:
                ffmpeg->setSpeed(dvalue);
                break;
            case 3:
                ffmpeg->setMute(value);
                break;
            case 4:
                ffmpeg->setVolume(value);
                break;
        }
    }
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_yyl_jni_player_YPlayer_n_1samplerate(JNIEnv *env, jobject instance) {
    if (ffmpeg != NULL) {
        return ffmpeg->getSampleRate();
    }
    return 0;

}
extern "C"
JNIEXPORT void JNICALL
Java_com_yyl_jni_player_YPlayer_record(JNIEnv *env, jobject instance, jboolean isRecord) {
    if (ffmpeg != NULL) {
        ffmpeg->startRecord(isRecord);
    }

}

extern "C"
JNIEXPORT jlong JNICALL
Java_com_yyl_jni_player_YPlayer_getDuration(JNIEnv *env, jobject instance) {
    if (ffmpeg != NULL) {
        return ffmpeg->getDuration();
    }
    return 0;
}
extern "C"
JNIEXPORT jdouble JNICALL
Java_com_yyl_jni_player_YPlayer_getCurrentTime(JNIEnv *env, jobject instance) {
    if (ffmpeg != NULL) {
        return ffmpeg->getCurrentTime();
    }
    return 0;
}